package tk.mybatis.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.common.KeCongResult;
import tk.mybatis.springboot.common.ResultObject;
import tk.mybatis.springboot.entity.CustomizedTask;
import tk.mybatis.springboot.entity.LogTask;
import tk.mybatis.springboot.service.TaskService;
import tk.mybatis.springboot.service.impl.LogExtendService;
import tk.mybatis.springboot.service.impl.TaskExtendService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class TaskController {

    @Autowired
    TaskService taskService;

    @Autowired
    TaskExtendService taskExtendService;

    @Autowired
    LogExtendService logExtendService;

    @RequestMapping("/api/pauseTask")
    public Object pauseTask(HttpSession session , @RequestBody Map<String,String> map){
        String taskNo = map.get("TaskNo");
        if(taskNo == null){
            ResultObject resultObject = new ResultObject();
            resultObject.code = 401;
            resultObject.message = "没有TaskNo";
            return resultObject;
        }
        //CustomizedTask currentTask = (CustomizedTask)session.getAttribute("task");
        KeCongResult keCongResult = taskService.pauseTask(taskNo);
        ResultObject resultObject = new ResultObject();
        resultObject.data = keCongResult;
        return resultObject;
    }

    @RequestMapping("/api/resumeTask")
    public Object resumeTask(HttpSession session,@RequestBody Map<String,String> map){
       // CustomizedTask currentTask = (CustomizedTask)session.getAttribute("task");
        String taskNo = map.get("TaskNo");
        if(taskNo == null){
            ResultObject resultObject = new ResultObject();
            resultObject.code = 401;
            resultObject.message = "没有TaskNo";
            return resultObject;
        }
        KeCongResult keCongResult = taskService.resumeTask(taskNo);
        ResultObject resultObject = new ResultObject();
        resultObject.data = keCongResult;
        return resultObject;
    }

    @RequestMapping("/api/completeTask")
    public Object completeTask(@RequestBody Map<String,String> map){
        String taskNo = map.get("TaskNo");
        if(taskNo == null){
            ResultObject resultObject = new ResultObject();
            resultObject.code = 401;
            resultObject.message = "没有TaskNo";
            return resultObject;
        }
        KeCongResult keCongResult = taskService.completeTask(taskNo);
        ResultObject resultObject = new ResultObject();
        resultObject.data = keCongResult;
        return resultObject;
    }

    @RequestMapping("/api/selectTaskByPage2")
    public Object getAllTask(@RequestParam(name="page") Integer page, @RequestParam(name="count") Integer count,
                             @RequestParam(name="tab") Integer tab){
        List<CustomizedTask> customizedTasks = taskExtendService.selectTaskByPage(page, count,tab);
        ResultObject resultObject = new ResultObject();
        resultObject.data = customizedTasks;
        return resultObject;
    }

    @RequestMapping("/api/selectLogByPage")
    public Object getAllLog(@RequestParam(name="page") Integer page, @RequestParam(name="count") Integer count
                            ){
        List<LogTask> logTasks = logExtendService.selectLogByPage(page, count);

        ResultObject resultObject = new ResultObject();
        resultObject.data = logTasks;
        return resultObject;
    }

}
